﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using BLL;
using DAL;
using Models;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;


public partial class backstage_mail : System.Web.UI.Page
{
    protected Mail ma = new Mail();
    protected MailManager mm = new MailManager();


    // Methods
    protected void btnSendMail_Click(object sender, EventArgs e)
    {
        SendMailMessage message = new SendMailMessage();
        try
        {
            message.subject = "邮件测试";
            message.body = "邮件测试";
            message.html = true;
            message.mailTo = this.txtMailTo.Value;
            message.from = this.txtFromAddress.Value;
            message.fromName = this.txtFromName.Value;
            message.mailDomain = this.txtMailDomain.Value;
            message.mailDomainPort = Convert.ToInt32(this.txtMailDomainPort.Value);
            message.mailServerUserName = this.txtMailServerUserName.Value;
            message.mailServerPassWord = this.txtMailServerPassWord.Value;
            if (message.RuleSend())
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "send", "<script language=\"javascript\" defer>alert(\"发送成功！\");document.getElementById(\"txtMailServerPassWord\").value=\"" + message.mailServerPassWord + "\";document.getElementById(\"sendMail\").style.display=\"block\";document.getElementById(\"btnMailTest\").style.display=\"none\";</script>");
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "send", "<script language=\"javascript\" defer>alert(\"发送失败！\");document.getElementById(\"txtMailServerPassWord\").value=\"" + message.mailServerPassWord + "\";document.getElementById(\"sendMail\").style.display=\"block\";document.getElementById(\"btnMailTest\").style.display=\"none\";</script>");
            }
        }
        catch
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "send", "<script language=\"javascript\" defer>alert(\"发送失败！\");document.getElementById(\"txtMailServerPassWord\").value=\"" + message.mailServerPassWord + "\";document.getElementById(\"sendMail\").style.display=\"block\";document.getElementById(\"btnMailTest\").style.display=\"none\";</script>");
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
        try
        {
            this.ma.FromAddress = this.txtFromAddress.Value;
            this.ma.FromName = this.txtFromName.Value;
            this.ma.MailDomain = this.txtMailDomain.Value;
            this.ma.MailDomainPort = this.txtMailDomainPort.Value;
            this.ma.MailServerUserName = this.txtMailServerUserName.Value;
            this.ma.MailServerPassWord = this.txtMailServerPassWord.Value;
            this.ma.IsEnabled = Convert.ToInt32(base.Request.Form["isEnabled"]);
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                this.ma.Id = Convert.ToInt32(base.Request.QueryString["id"]);
                if (this.mm.ModifyMail(this.ma))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"mail_list.aspx\";</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");document.getElementById(\"txtMailServerPassWord\").value=\"" + this.ma.MailServerPassWord + "\";</script>");
                }
            }
            else if (this.mm.AddMail(this.ma))
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加成功！\");location.href=\"mail_list.aspx\";</script>");
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");document.getElementById(\"txtMailServerPassWord\").value=\"" + this.ma.MailServerPassWord + "\";</script>");
            }
        }
        catch
        {
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");document.getElementById(\"txtMailServerPassWord\").value=\"" + this.ma.MailServerPassWord + "\";</script>");
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");document.getElementById(\"txtMailServerPassWord\").value=\"" + this.ma.MailServerPassWord + "\";</script>");
            }
        }
    }

    public StringBuilder GetTitle()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                builder.AppendLine("编辑邮件");
                return builder;
            }
            builder.AppendLine("添加邮件");
        }
        catch
        {
        }
        return builder;
    }

    private void MailBind()
    {
        try
        {
            DataTable mailById = this.mm.GetMailById(Convert.ToInt32(base.Request.QueryString["id"]));
            if (mailById.Rows.Count > 0)
            {
                this.txtFromAddress.Value = mailById.Rows[0]["fromAddress"].ToString();
                this.txtFromName.Value = mailById.Rows[0]["fromName"].ToString();
                this.txtMailDomain.Value = mailById.Rows[0]["mailDomain"].ToString();
                this.txtMailDomainPort.Value = mailById.Rows[0]["mailDomainPort"].ToString();
                this.txtMailServerUserName.Value = mailById.Rows[0]["mailServerUserName"].ToString();
                base.ClientScript.RegisterStartupScript(base.ClientScript.GetType(), "ssm", "<script language=\"javascript\" defer>document.getElementById(\"txtMailServerPassWord\").value=\"" + mailById.Rows[0]["mailServerPassWord"].ToString() + "\";</script>");
                if (Convert.ToInt32(mailById.Rows[0]["isEnabled"]) == 1)
                {
                    this.rdIsEnabled1.Checked = true;
                }
                else
                {
                    this.rdIsEnabled0.Checked = true;
                }
            }
        }
        catch
        {
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
        }
        if (!base.IsPostBack && (base.Request.QueryString["ot"] != null))
        {
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                this.MailBind();
            }
            else
            {
                this.rdIsEnabled1.Checked = true;
            }
        }
    }

}